package com.expdatacloud.controller.export;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.expdatacloud.dto.OverdueMaterialDto;
import com.expdatacloud.service.DifferenceMSAService;
import com.expdatacloud.service.ReportsService;
import com.expdatacloud.vo.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author jie
 * @date 2019/12/19 -11:44
 */
@RestController
public class ExportController {

    @Autowired
    private DifferenceMSAService differenceMSAService;

    @Autowired
    private ReportsService reportsService;

    /**
     * 质检合格未上架看板
     */
    @GetMapping("/exportUnQualifiedQuality")
    public void exportUnQualifiedQuality(HttpServletResponse response, HttpSession session) {
        List<UnQualifiedQualityVo> list = (List<UnQualifiedQualityVo>) session.getAttribute("unQualifiedQualityList");

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("质检合格未上架看板", "质检合格未上架看板"),
                UnQualifiedQualityVo.class, list);
        ioExport(response, workbook, "质检合格未上架看板.xls");
    }

    /**
     * 质检不合格未上架看板
     */
    @GetMapping("/exportQualifiedQuality")
    public void exportQualifiedQuality(HttpServletResponse response, HttpSession session) {
        List<UnQualifiedQualityVo> list = (List<UnQualifiedQualityVo>) session.getAttribute("qualifiedQualityList");

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("质检不合格未上架看板", "质检不合格未上架看板"),
                UnQualifiedQualityVo.class, list);
        ioExport(response, workbook, "质检不合格未上架看板.xls");
    }

    /**
     * 收获日对账
     */
    @GetMapping("/exportReconciliation")
    public void exportReconciliation(HttpServletResponse response, HttpSession session) {
        List<ReceivingReconciliationVo> list = (List<ReceivingReconciliationVo>) session.getAttribute("reconciliationList");

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("收获日对账", "收获日对账"),
                ReceivingReconciliationVo.class, list);
        ioExport(response, workbook, "收获日对账.xls");
    }

    /**
     * iqc 履历日对账统计表
     */
    @GetMapping("/exportIqcHistoryReconciliation")
    public void exportIqcHistoryReconciliation(HttpServletResponse response, HttpSession session) {
        List<IqcHistoryReconciliationVo> list = (List<IqcHistoryReconciliationVo>) session.getAttribute("iqcHistoryReconciliationList");

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("IQC履历日对账统计表", "IQC履历日对账统计表"),
                IqcHistoryReconciliationVo.class, list);
        ioExport(response, workbook, "IQC履历日对账统计表.xls");
    }

    /**
     * T100库存与WMS库存差异
     */
    @GetMapping("/exportStoreDifference")
    public void exportStoreDifference(HttpServletResponse response, HttpSession session,
                                      String storerkey,
                                      String sku,
                                      String lottable01,
                                      String lottable02,
                                      String differ) {
        List<StoreDifferenceVo> list = differenceMSAService.getDiffStore2(storerkey, sku, lottable01, lottable02, differ);

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("T100库存与WMS库存差异", "T100库存与WMS库存差异"),
                StoreDifferenceVo.class, list);
        ioExport(response, workbook, "T100库存与WMS库存差异.xls");
    }

    /**
     * IQC来料明细表
     */
    @GetMapping("/exportSuppliedMaterials")
    public void exportSuppliedMaterialsList(HttpServletResponse response, HttpSession session) {
        List<SuppliedMaterialsDailyVo> list = (List<SuppliedMaterialsDailyVo>) session.getAttribute("suppliedMaterialsList");

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("IQC来料明细表", "IQC来料明细表"),
                SuppliedMaterialsDailyVo.class, list);
        ioExport(response, workbook, "IQC来料明细表.xls");
    }


    /**
     * IQC来料检验十差供应商表
     */
    @GetMapping("/exportSuppliedMaterialsBadTop10List")
    public void exportSuppliedMaterialsBadTop10List(HttpServletResponse response, HttpSession session) {
        List<SuppliedMaterialsBadTop10Vo> list = (List<SuppliedMaterialsBadTop10Vo>) session.getAttribute("suppliedMaterialsBadTop10List");

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("IQC来料检验十差供应商", "IQC来料检验十差供应商"),
                SuppliedMaterialsBadTop10Vo.class, list);
        ioExport(response, workbook, "IQC来料检验十差供应商.xls");
    }

    /**
     * IQC来料检验十佳供应商表
     */
    @GetMapping("/exportSuppliedMaterialsGoodTop10List")
    public void exportSuppliedMaterialsGoodTop10List(HttpServletResponse response, HttpSession session) {
        List<SuppliedMaterialsBadTop10Vo> list = (List<SuppliedMaterialsBadTop10Vo>) session.getAttribute("suppliedMaterialsGoodTop10List");

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("IQC来料检验十佳供应商", "IQC来料检验十佳供应商"),
                SuppliedMaterialsBadTop10Vo.class, list);
        ioExport(response, workbook, "IQC来料检验十佳供应商.xls");
    }

    /**
     * Iqc来料月报导出
     */
    @GetMapping("/exportSuppliedMaterialsCheckMonthReport")
    public void exportSuppliedMaterialsCheckMonthReport(HttpServletResponse response, HttpSession session) {

        //获取数据
        List<Integer> monthList = (List<Integer>) session.getAttribute("monthList");
        List<SuppliedMaterialsCheckMonthReportVo> suppliedMaterialsCheckMonthReportVoList = (List<SuppliedMaterialsCheckMonthReportVo>) session.getAttribute("suppliedMaterialsCheckMonthReportVo");
        Map<String, Object> sumMap = (Map<String, Object>) session.getAttribute("sumMap");

        /* 设置单元格样式*/
        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作表
        HSSFSheet sheet = workbook.createSheet("Iqc来料月报");
        //设置默认行宽
        sheet.setDefaultColumnWidth(30);
        //合并的单元格样式
        HSSFCellStyle boderStyle = workbook.createCellStyle();
        //单元格居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);

        /* 设置表头*/
        HSSFRow row = sheet.createRow((int) 1);
        row.setHeightInPoints(20);
        HSSFCell cellHead = row.createCell(0);
        cellHead.setCellStyle(cellStyle);
        cellHead.setCellValue("项目");
        for (int i = 0; i < monthList.size(); i++) {
            cellHead = row.createCell(i + 1);
            cellHead.setCellStyle(cellStyle);
            cellHead.setCellValue(monthList.get(i) + "月");
        }
        cellHead = row.createCell(monthList.size() + 1);
        cellHead.setCellStyle(cellStyle);
        cellHead.setCellValue("合计");

        //开始写入实体数据信息
        HSSFCell cell = null;
        for (int k = 2; k < 7; k++) {
            for (int i = 0; i < suppliedMaterialsCheckMonthReportVoList.size(); i++) {
                //列的下标
                int columnIndex = 0;

                //行
                HSSFRow roww = sheet.createRow(k);

                roww.setHeightInPoints(20);
                switch (k) {
                    case 2:
                        HSSFCell cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue("来料总批数");
                        for (int j = 0; j < monthList.size(); j++) {
                            cell1 = roww.createCell(columnIndex++);
                            cell1.setCellStyle(cellStyle);
                            cell1.setCellValue(suppliedMaterialsCheckMonthReportVoList.get(j).getCheckQty());
                        }
                        cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue(String.valueOf(sumMap.get("checkQtySum")));
                        break;
                    case 3:
                        cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue("合格批数");
                        for (int j = 0; j < monthList.size(); j++) {
                            cell1 = roww.createCell(columnIndex++);
                            cell1.setCellStyle(cellStyle);
                            cell1.setCellValue(suppliedMaterialsCheckMonthReportVoList.get(j).getOkQty());
                        }
                        cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue(String.valueOf(sumMap.get("okQtySum")));
                        break;
                    case 4:
                        cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue("不合格批数");
                        for (int j = 0; j < monthList.size(); j++) {
                            cell1 = roww.createCell(columnIndex++);
                            cell1.setCellStyle(cellStyle);
                            cell1.setCellValue(suppliedMaterialsCheckMonthReportVoList.get(j).getNgQty());
                        }
                        cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue(String.valueOf(sumMap.get("ngQtySum")));
                        break;
                    case 5:
                        cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue("合格率");
                        for (int j = 0; j < monthList.size(); j++) {
                            cell1 = roww.createCell(columnIndex++);
                            cell1.setCellStyle(cellStyle);
                            cell1.setCellValue(suppliedMaterialsCheckMonthReportVoList.get(j).getPercentOfPass() + "%");
                        }
                        cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue(String.valueOf(sumMap.get("percentOfPassSum")));
                        break;
                    case 6:
                        cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue("目标值<99.00%");
                        for (int j = 0; j < monthList.size(); j++) {
                            cell1 = roww.createCell(columnIndex++);
                            cell1.setCellStyle(cellStyle);
                            cell1.setCellValue("99.00%");
                        }
                        cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue("99.00%");
                        break;
                    default:
                        break;
                }
            }
        }
        ioExport(response, workbook, "Iqc来料月报.xls");

    }


    /**
     * IQC来料周报
     */
    @GetMapping("/exportSuppliedMaterialsCheckWeekReport")
    public void exportSuppliedMaterialsCheckWeekReport(HttpServletResponse response, HttpSession session) {

        //获取数据
        List<Integer> weekList = (List<Integer>) session.getAttribute("weekList");
        List<SuppliedMaterialsCheckWeekReportVo> suppliedMaterialsCheckWeekReportList = (List<SuppliedMaterialsCheckWeekReportVo>) session.getAttribute("suppliedMaterialsCheckWeekReportList");

        /* 设置单元格样式*/
        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作表
        HSSFSheet sheet = workbook.createSheet("IQC来料周报");
        //设置默认行宽
        sheet.setDefaultColumnWidth(30);
        //合并的单元格样式
        HSSFCellStyle boderStyle = workbook.createCellStyle();
        //单元格居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);

        /* 设置表头*/
        HSSFRow row = sheet.createRow((int) 1);
        row.setHeightInPoints(20);
        HSSFCell cellHead = row.createCell(0);
        cellHead.setCellValue("项目");
        cellHead.setCellStyle(cellStyle);

        for (int i = 0; i < weekList.size(); i++) {
            cellHead = row.createCell(i + 1);
            cellHead.setCellStyle(cellStyle);
            cellHead.setCellValue(weekList.get(i) + "周");
        }

        //开始写入实体数据信息
        HSSFCell cell = null;
        for (int k = 2; k < 7; k++) {
            for (int i = 0; i < suppliedMaterialsCheckWeekReportList.size(); i++) {
                //列的下标
                int columnIndex = 0;

                //行
                HSSFRow roww = sheet.createRow(k);


                roww.setHeightInPoints(20);
                switch (k) {
                    case 2:
                        HSSFCell cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue("送检批数");
                        for (int j = 0; j < weekList.size(); j++) {
                            cell1 = roww.createCell(columnIndex++);
                            cell1.setCellStyle(cellStyle);
                            cell1.setCellValue(suppliedMaterialsCheckWeekReportList.get(j).getCheckQty());
                        }
                        break;
                    case 3:
                        cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue("合格批数");
                        for (int j = 0; j < weekList.size(); j++) {
                            cell1 = roww.createCell(columnIndex++);
                            cell1.setCellStyle(cellStyle);
                            cell1.setCellValue(suppliedMaterialsCheckWeekReportList.get(j).getOkQty());
                        }
                        break;
                    case 4:
                        cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue("退货批数");
                        for (int j = 0; j < weekList.size(); j++) {
                            cell1 = roww.createCell(columnIndex++);
                            cell1.setCellStyle(cellStyle);
                            cell1.setCellValue(suppliedMaterialsCheckWeekReportList.get(j).getSalesReturnQty());
                        }
                        break;
                    case 5:
                        cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue("批退拣货目标<0.5%");
                        for (int j = 0; j < weekList.size(); j++) {
                            cell1 = roww.createCell(columnIndex++);
                            cell1.setCellStyle(cellStyle);
                            cell1.setCellValue("0.50%");
                        }
                        break;
                    case 6:
                        cell1 = roww.createCell(columnIndex++);
                        cell1.setCellStyle(cellStyle);
                        cell1.setCellValue("批退次率");
                        for (int j = 0; j < weekList.size(); j++) {
                            cell1 = roww.createCell(columnIndex++);
                            cell1.setCellStyle(cellStyle);
                            cell1.setCellValue(suppliedMaterialsCheckWeekReportList.get(j).getLotRejectRate() + "%");
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        ioExport(response, workbook, "IQC来料周报.xls");

    }

    /**
     * IQC来料周报-按物料大类退货
     */
    @GetMapping("/exportSuppliedMaterialsCheckWeekByUDF8ReportReport")
    public void exportSuppliedMaterialsCheckWeekByUDF8ReportReport(HttpServletResponse response, HttpSession session) {

        //获取数据
        List<Integer> weekList = (List<Integer>) session.getAttribute("weekList");
        List<SuppliedMaterialsCheckWeekReportVo> sumList = (List<SuppliedMaterialsCheckWeekReportVo>) session.getAttribute("sumList");
        List<String> udf8List = (List<String>) session.getAttribute("udf8List");
        List<SuppliedMaterialsCheckWeekReportVo> suppliedMaterialsCheckWeekReportList = (List<SuppliedMaterialsCheckWeekReportVo>) session.getAttribute("suppliedMaterialsCheckWeekReportList");

        /* 设置单元格样式*/
        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作表
        HSSFSheet sheet = workbook.createSheet("IQC来料周报-按物料大类退货");
        //设置默认行宽
        sheet.setDefaultColumnWidth(30);
        //合并的单元格样式
        HSSFCellStyle boderStyle = workbook.createCellStyle();
        //单元格居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);

        /* 设置表头*/
        HSSFRow row = sheet.createRow((int) 1);
        row.setHeightInPoints(20);
        HSSFCell cellHead = row.createCell(0);
        cellHead.setCellStyle(cellStyle);
        cellHead.setCellValue("物料大类");
        for (int i = 0; i < weekList.size(); i++) {
            cellHead = row.createCell(i + 1);
            cellHead.setCellStyle(cellStyle);
            cellHead.setCellValue(weekList.get(i) + "周");
        }

        cellHead = row.createCell(weekList.size() + 1);
        cellHead.setCellStyle(cellStyle);
        cellHead.setCellValue("合计");
        //开始写入实体数据信息
        HSSFCell cell = null;

        for (int i = 0; i < udf8List.size(); i++) {

            //列的下标 每次右移 1位
            int columnIndex = 0;

            //行
            HSSFRow roww = sheet.createRow(i + 2);

            roww.setHeightInPoints(20);

            //物料大类
            HSSFCell cell1 = roww.createCell(columnIndex++);
            cell1.setCellStyle(cellStyle);
            cell1.setCellValue(udf8List.get(i));

            for (SuppliedMaterialsCheckWeekReportVo suppliedMaterialsCheckWeekReportVo : suppliedMaterialsCheckWeekReportList) {
                if (udf8List.get(i).equals(suppliedMaterialsCheckWeekReportVo.getUdf8())) {
                    cell1 = roww.createCell(columnIndex++);
                    cell1.setCellStyle(cellStyle);
                    cell1.setCellValue(suppliedMaterialsCheckWeekReportVo.getSalesReturnQty());
                }
            }
            cell1 = roww.createCell(columnIndex++);
            cell1.setCellStyle(cellStyle);
            cell1.setCellValue(sumList.get(i).getSumQty());
        }
        ioExport(response, workbook, "IQC来料周报-按物料大类退货.xls");
    }

    /**
     * 超期物料重检表
     */
    @GetMapping("/exportOverdueMaterial")
    public void exportOverdueMaterial(HttpServletResponse response,
                                      OverdueMaterialDto overdueMaterialDto) {


        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("超期物料重检表", "超期物料重检表"),
                OverdueMaterialVo.class,  reportsService.getOverdueMaterialList(overdueMaterialDto));
        ioExport(response, workbook, "超期物料重检表.xls");
    }

    /**
     * Bin卡明细表
     */
    @GetMapping("/exportBinitrnList")
    public void exportBinitrnList(HttpServletResponse response, HttpSession session) {
        List<YtBinitrnVo> list = (List<YtBinitrnVo>) session.getAttribute("binitrnList");

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("Bin卡明细列表", "Bin卡明细列表"),
                YtBinitrnVo.class, list);
        ioExport(response, workbook, "Bin卡明细列表.xls");
    }


    /**
     * 导出io流
     *
     * @param filename 文件名
     */
    private void ioExport(HttpServletResponse response, Workbook workbook, String filename) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
